/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DeltaInputPanel.java
 *
 * Created on 16.01.2012, 11:05:24
 */
package promogui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.util.EventObject;
import javax.swing.AbstractAction;
import javax.swing.AbstractCellEditor;
import javax.swing.ImageIcon;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.ListCellRenderer;
import javax.swing.UIManager;
import javax.swing.event.CellEditorListener;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableColumn;
import javax.swing.text.JTextComponent;

/**
 *
 * @author Cryptkeeper
 */
public class InputTablePanel extends javax.swing.JPanel {

    private Engineer engineer;
    private Project project;
    private IInputTableModel model;
    
    /** Creates new form DeltaInputPanel */
    public InputTablePanel(IInputTableModel model, boolean locked) {
        
        //this.project = project;
        //this.engineer = engineer;
        
        initComponents();
        
        //model = new DeltaInputTableModel(project, engineer);
        
        this.model = model;
        
        JList rowHeader = new JList(model.getRowHeaderModel());
        rowHeader.setBackground(new Color(214,217,223));
        rowHeader.setFixedCellWidth(60);
        rowHeader.setFixedCellHeight(deltaValueTable.getRowHeight());
        rowHeader.setCellRenderer(new RowHeaderRenderer(deltaValueTable));
        
        deltaValueTable.setModel(model);
        
        this.jToolBar.setFloatable(false);

        AddAction addAction = new AddAction();
        DeleteAction delAction = new DeleteAction();
        
        this.jToolBar.add(addAction);
        this.jToolBar.add(delAction);
        
        
        
        this.scrollPane.setRowHeaderView(rowHeader);
        
        if ((!model.isEditabe()) || locked == true)
        {
            addAction.setEnabled(false);
            delAction.setEnabled(false);
            //addWeek.setEnabled(false);
            //removeWeek.setEnabled(false);
            deltaValueTable.setEnabled(false);
        }
        
    }
    
    class MyJTable extends JTable
    {        
        @Override
        public Component prepareEditor(TableCellEditor editor, int row, int column) {
        Component c = super.prepareEditor(editor, row, column);
        if (c instanceof JTextComponent) {
            ((JTextComponent) c).selectAll();
        } 
        return c;
}
    }
    
    class AddAction extends AbstractAction {

        public AddAction() {
          super("Home", new ImageIcon("add.png"));
          putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke('H',
              Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
          putValue(SHORT_DESCRIPTION, "Woche hinzufügen");
        }

        public void actionPerformed(ActionEvent e) {
            model.insertWeek();
        }
      }
    
        class DeleteAction extends AbstractAction {

        public DeleteAction() {
          super("Home", new ImageIcon("delete.png"));
          putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke('H',
              Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
          putValue(SHORT_DESCRIPTION, "Letzte Woche löschen");
        }

        public void actionPerformed(ActionEvent e) {
            model.removeLastWeek();
        }
      }

    class RowHeaderRenderer extends JLabel implements ListCellRenderer
    {
        RowHeaderRenderer(JTable table)
        {
          JTableHeader tableHeader = table.getTableHeader();
          setBorder(UIManager.getBorder("TableHeader.cellBorder"));
          setHorizontalAlignment(CENTER);
          setForeground(tableHeader.getForeground());
          //setBackground(Color.white);
          //setOpaque(true);  // Damit der Hintergrund nicht verändert wird
          setFont(tableHeader.getFont());
        }

        public Component getListCellRendererComponent(JList list, Object value, int index, 
                                                     boolean fSelected, boolean fCellHasFocus)
        {
          setText((value == null) ? "" : value.toString());
          return this;
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scrollPane = new javax.swing.JScrollPane();
        deltaValueTable = new MyJTable();
        jToolBar = new javax.swing.JToolBar();

        setLayout(new java.awt.BorderLayout());

        deltaValueTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        deltaValueTable.setCellSelectionEnabled(true);
        scrollPane.setViewportView(deltaValueTable);

        add(scrollPane, java.awt.BorderLayout.CENTER);

        jToolBar.setRollover(true);
        add(jToolBar, java.awt.BorderLayout.PAGE_END);
    }// </editor-fold>//GEN-END:initComponents

    public void addWeek()
    {
        model.insertWeek();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable deltaValueTable;
    private javax.swing.JToolBar jToolBar;
    private javax.swing.JScrollPane scrollPane;
    // End of variables declaration//GEN-END:variables
}

